import json
import ast


# 一级菜单
def menu():
    global username
    global password
    print("用户管理系统")
    choice1 = input("菜单：\n1.登录\n2.退出\n:")
    if choice1 == "登录" or choice1 == "1":
        login()
    else:
        exit(0)


# 登录系统
def login():
    global username
    global password
    global user_list
    global root_list
    global all_user

    with open("user-list.txt", "r+") as fp, \
            open("root-list.txt", "r+") as f, \
            open("all_user.txt", "r+") as q:
        a = f.read()
        root_list = ast.literal_eval(a)
        b = fp.read()
        user_list = ast.literal_eval(b)
        c = q.read()
        all_user = ast.literal_eval(c)

        # 判断用户名为管理员还是普通用户

        for i in range(3):
            username = input("请输入您的用户名:")
            if username in root_list:
                for j in range(3):
                    password = input("请输入您的密码:")
                    for x, y in root_list.items():
                        a = y.get('password')
                        if password == a:
                            print("_" * 15)
                            print(f"恭喜{username}登录成功")
                            print("_" * 15)
                            super()
                            break
                        else:
                            if j == 2:
                                print('-' * 15)
                                print('请5分钟后再进行登录')
                                print('-' * 15)
                                exit(0)
                            else:
                                print('-' * 15)
                                print(f"请输入正确的密码,您还有{2 - j}次机会输入密码")
                                print('-' * 15)
                else:
                    if i == 2:
                        print('-' * 15)
                        print('请5分钟后再进行登录')
                        print('-' * 15)
                        exit(0)
                    else:
                        print('-' * 15)
                        print(f"您还有{2 - i}次机会登录")
                        print('-' * 15)
            elif username in user_list:
                for w in range(3):
                    password = input("请输入您的密码:")
                    for k, l in user_list.items():
                        b = l.get('password')
                        if password == b:
                            print("_" * 15)
                            print(f"恭喜{username}登录成功")
                            print("_" * 15)
                            normal()
                            break
                        else:
                            if w == 2:
                                print('-' * 15)
                                print('请5分钟后再进行登录')
                                print('-' * 15)
                                exit(0)
                            else:
                                print('-' * 15)
                                print(f"请输入正确的密码,您还有{2 - w}次机会输入密码")
                                print('-' * 15)
                            break
                else:
                    if i == 2:
                        print('-' * 15)
                        print('请5分钟后再进行登录')
                        print('-' * 15)
                        exit(0)
                    else:
                        print('-' * 15)
                        print(f"您还有{2 - i}次机会登录")
                        print('-' * 15)
                break


# 普通用户操作
def normal():
    global username
    global password

    while True:
        choice2 = input("1.修改当前爱好\n2.返回一级菜单\n3.退出\n:")
        if choice2 == "修改当前爱好" or choice2 == "1":
            hobby_change = input("请输入您要修改的爱好：")

            for i, j in user_list.items():

                change = {f"{username}": {"name": username, "password": j.get('password'), "hobby": hobby_change}}
                user_list.update(change)
                with open("user-list.txt", "w+") as p, \
                        open("all_user.txt", "w+") as pf:
                    b = str(user_list)
                    d = b.replace("\'", "\"")
                    p.write(d)
                    pf.write(d)
                print("修改成功")

        elif choice2 == "返回一级菜单" or choice2 == "2":
            menu()
        else:
            exit(0)


# 查看
def check():
    for u, o in root_list.items():
        if u == username:
            print("姓名      密码      爱好")
            print(f'{u}   {o.get("password")}   {o.get("hobby")}')


# 修改
def alter():
    username_change = input("请输入要修改的用户名:")
    # 修改管理员的信息
    if username_change in root_list:
        passwd_change = input("请输入要修改的密码:")
        hobby_change = input("请输入要修改的爱好:")
        change = {f"{username_change}": {"name": username_change, "password": passwd_change, "hobby": hobby_change}}
        user_list.update(change)

        with open("root-list.txt", "w+") as f, \
                open("all_user.txt", "w+") as fp:
            a = str(user_list)
            c = a.replace("\'", "\"")
            f.write(c)
            fp.write(c)

    # 修改普通用户的信息
    else:
        passwd_change = input("请输入要修改的密码:")
        hobby_change = input("请输入要修改的爱好:")
        change = {f"{username_change}": {"name": username_change, "password": passwd_change, "hobby": hobby_change}}
        user_list.update(change)
        with open("user-list.txt", "w+") as p, \
                open("all_user.txt", "w+") as pf:
            b = str(user_list)
            d = b.replace("\'", "\"")
            p.write(d)
            pf.write(d)
        print("修改成功")


# 删除用户
def delete_user():
    user_remove = input("请输入要删除的用户")
    # 删除管理员用户
    if user_remove in root_list:
        with open("root-list.txt", "w+") as fp, \
                open("all_user.txt", "w+") as f:
            e = user_list.pop(f'{user_remove}')
            g = str(e)
            h = g.replace("\'", "\"")
            # 删除管理员里的此用户
            fp.write(h)
            # 删除所有用户里的此用户
            f.write(h)
            print("删除成功")

    # 删除普通用户
    elif user_remove in user_list:
        with open("user-list.txt", "w+") as fpp, \
                open("all_user.txt", "w+") as pf:
            r = user_list.pop(f'{user_remove}')
            t = str(r)
            p = t.replace("\'", "\"")
            # 删除普通用户里的此用户
            fpp.write(p)
            # 删除所有用户里的此用户
            pf.write(p)
            print("删除成功")

    else:
        print("此用户不存在")




# 添加
def add_user():
    user_add = input("请输入要添加的用户名")

    # 判断用户是否已存在
    if user_add in all_user:
        print("已有用户不能添加")
    else:
        passwd_add = input("请输入用户密码")
        hobby_add = input("请输入用户爱好")

        identity_add = input("请选择用户身份：\n"
                             "1.管理员\n"
                             "2.普通用户")
        # 存入不同用户列表
        if identity_add == "管理员" or identity_add == "1":
            with open("root-list.txt", "w+") as fp, \
                    open("all_user.txt", "w+") as f:
                add = {f"{user_add}": {"name": user_add, "password": passwd_add,
                                       "hobby": hobby_add}}
                root_list.update(add)
                b = str(root_list)
                c = b.replace("\'", "\"")
                # 加入管理员
                fp.write(c)
                # 加入所有用户
                f.write(c)

        else:
            with open("user-list.txt", "w+") as pf, \
                    open("all_user.txt", "w+") as p:
                add = {f"{user_add}": {"name": user_add, "password": passwd_add,
                                       "hobby": hobby_add}}
                root_list.update(add)
                d = str(user_list)
                e = d.replace("\'", "\"")
                # 加入普通用户里的此用户
                pf.write(e)
                # 加入所有用户里的此用户
                p.write(e)
        print("_"*15)
        print(f"成功添加用户{user_add}")
        print("_" * 15)


# 管理员用户操作
def super():
    global username
    global user_info
    global root_info
    while True:
        choice3 = input("请输入您的操作：\n"
                        "1.查看当前用户\n"
                        "2.修改某位用户\n"
                        "3.删除某位用户\n"
                        "4.添加某位用户\n"
                        "5.返回上一级菜单\n"
                        "6.退出\n:")

        # 查看用户信息
        if choice3 == "查看当前用户" or choice3 == "1":
            check()


        # 修改用户信息
        elif choice3 == "修改某位用户" or choice3 == "2":
            alter()

        # 删除用户
        elif choice3 == "删除某位用户" or choice3 == "3":
            delete_user()


        # 添加用户
        elif choice3 == "添加某位用户" or choice3 == "4":
            add_user()

        # 返回上一级
        elif choice3 == "返回上一级菜单" or choice3 == "5":
            menu()

        else:
            exit(0)


menu()
